<?
//Gallery 0.0.2
class sym_gallery extends symbiont{
    public function ___main($design="", $category=''){
        global $kernel, $sym, $sql;
        if(!$category&&$kernel->link->param1){
            $category=$kernel->link->param1;
        }
        if($category){
            $design=$this->_check($design, 'main');
            $d=new design($design, 'gallery', false);
            $this->gallerys='';
            $category=data::word($category);
            $this->category_id=$sql->select_each('sym_category', 'id', array('name'=>$category), '', '', 1);
            
            $gallerys=$sql->query_each("SELECT v.id, v.cover, v.name, v.image, p.title, p.content as description  FROM `sym_gallery` as v LEFT JOIN `paragraphs` as p ON v.paragraph_id=p.id WHERE v.category_id='".$this->category_id."' AND (p.lng_id = ".$kernel->lang->id." OR p.title IS NULL) ORDER BY v.position");
            
            
            $this->pages='';
            $this->content='';
            $this->count=1;
            if(is_array($gallerys)){
                $i=0;
                foreach($gallerys as $val){
                    $i++;
                    $this->id=$val['id'];
                    $this->name=$val['name'];
                    $this->title=$val['title']?$val['title']:$val['name'];
                    $this->cover=$val['cover'];
                    $this->image=$val['image'];
                    $this->description=$val['description'];
                    $this->content.=design::run($this->item);
                }
            }
            $d->inc();
        }
        else{
            $kernel->symbiont_add('category');
            $sym->category->___main('gallery', 'gallery');
        }
    }
    public function ___category($design, $id=0){
        global $kernel, $sym;
        $kernel->symbiont_add('category');
        $sym->category->___main($design, 'gallery', $id);
    }
    public function ___admin($design){
        global $kernel, $sym;
        $kernel->symbiont_add('gallery-admin');
        $sym->gallery_admin->main('');
    }
    public function _administrate($symbiont){
        global $sql, $kernel;
        $design=$this->_check('administrate');
        $d=new design($design, 'gallery', false);
        $this->options='';
        $categories=$sql->query_each("
                            SELECT c.id, c.name, p.title
                            FROM `sym_category` as c
                                LEFT JOIN `paragraphs` as p
                                    ON c.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                            WHERE c.for='gallery'
                            ORDER BY p.title
                            ");
        if(is_array($categories)){
            foreach($categories as $category){
                if($symbiont['function']==$category['name']) $this->current=true;
                else $this->current=false;
                $this->id=$category['id'];
                $this->name=$category['name'];
                $this->title=$category['title'];
                $this->options.=design::run($this->option);
            }
        }
        
        $this->options_design='';
        $dir=opendir('design/'.$kernel->site->design.'/symbionts/gallery/');
        while($file=readdir($dir)){
            if(substr($file, 0, 4)=='main'){
                $name=substr($file, 0, -9);
                $this->name=$name;
                if(isset($symbiont['template'])&&$symbiont['template']==$name) $this->current=true;
                else $this->current=false;
                if($name=='main'){
                    $title=$name;
                }
                elseif($name[4]=='_'){
                    $title=substr($name, 5);
                }
                else{
                    $title=substr($name, 4);
                }
                $this->title=ucfirst(str_replace('_', '', $title));
                $this->options_design.=design::run($this->option_design);
            }
        }
        $d->inc();
    }
}
?>